package com.ruoyi.sxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;

import com.ruoyi.sxt.domain.SxtJueseCaidan;
import com.ruoyi.sxt.domain.SxtYonghu;
import com.ruoyi.sxt.domain.SxtYonghuJuese;
import com.ruoyi.sxt.mapper.SxtJueseCaidanMapper;
import com.ruoyi.sxt.mapper.SxtJueseMapper;
import com.ruoyi.sxt.mapper.SxtYonghuJueseMapper;
import com.ruoyi.sxt.mapper.SxtYonghuMapper;
import com.ruoyi.sxt.service.ISxtJueseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 角色信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-18
 */
@Service
public class SxtJueseServiceImpl implements ISxtJueseService
{
    @Autowired
    private SxtJueseMapper sxtJueseMapper;

    @Autowired
    private SxtJueseCaidanMapper sxtJueseCaidanMapper;

    @Autowired
    private SxtYonghuJueseMapper sxtYonghuJueseMapper;

    @Autowired
    private SxtYonghuMapper sxtYonghuMapper;



    /**
     * 查询角色信息
     * 
     * @param roleId 角色信息主键
     * @return 角色信息
     */
    @Override
    public SxtJuese selectSxtJueseByRoleId(Long roleId)
    {
        return sxtJueseMapper.selectSxtJueseByRoleId(roleId);
    }

    /**
     * 查询角色信息列表
     * 
     * @param sxtJuese 角色信息
     * @return 角色信息
     */
    @Override
    public List<SxtJuese> selectSxtJueseList(SxtJuese sxtJuese)
    {
        return sxtJueseMapper.selectSxtJueseList(sxtJuese);
    }

    @Override
    public List<SxtYonghu> allocatedYonghuList(SxtYonghu sxtYonghu) {
        List<SxtYonghu> sxtYonghuList = sxtYonghuMapper.allocatedYonghuList(sxtYonghu);
        return sxtYonghuList;
    }

    @Override
    public List<SxtYonghu> notAllocatedYonghuList(SxtYonghu sxtYonghu) {
        SxtYonghu sxtYonghu1 = new SxtYonghu();
        sxtYonghu1.setRoleId(sxtYonghu.getRoleId());
        SxtYonghu sxtYonghu22 = new SxtYonghu();
        sxtYonghu22.setUserName(sxtYonghu.getUserName());
        sxtYonghu22.setPhonenumber(sxtYonghu.getPhonenumber());
        List<SxtYonghu> sxtAllYonghuList = sxtYonghuMapper.selectSxtYonghuList(sxtYonghu22);
        List<SxtYonghu> sxtBindYonghuList = sxtYonghuMapper.allocatedYonghuList(sxtYonghu1);
        List<SxtYonghu> sxtYonghuList = new ArrayList<>();
        for(SxtYonghu sxtYonghu2 : sxtAllYonghuList){
            for(SxtYonghu sxtYonghu3 : sxtBindYonghuList){
                if(sxtYonghu2.getUserId() == sxtYonghu3.getUserId()){
                    sxtYonghu2.setFlag(true);
                    break;
                }
            }
        }
        for(SxtYonghu s : sxtAllYonghuList){
            if(s.isFlag() == false && s.getUserId() != 1){
                sxtYonghuList.add(s);
            }
        }

        return sxtYonghuList;
    }

    /**
     * 新增角色信息
     * 
     * @param sxtJuese 角色信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSxtJuese(SxtJuese sxtJuese)
    {
        sxtJuese.setCreateTime(DateUtils.getNowDate());
        int rows = sxtJueseMapper.insertSxtJuese(sxtJuese);
        insertJueseCaidan(sxtJuese);
        return rows;
    }

    @Override
    public int insertSxtYonghuJuese(Long roleId, Long[] userIds) {
        List<SxtYonghuJuese> yonghuJueseList = new ArrayList<>();
        for(Long userId : userIds){
            SxtYonghuJuese sxtYonghuJuese = new SxtYonghuJuese();
            sxtYonghuJuese.setRoleId(roleId);
            sxtYonghuJuese.setUserId(userId);
            yonghuJueseList.add(sxtYonghuJuese);
        }
        return sxtYonghuJueseMapper.batchYonghuJuese(yonghuJueseList);
    }

    public void insertJueseCaidan(SxtJuese sxtJuese){
        Long[] menuIds = sxtJuese.getMenuIds();
        if(menuIds != null && menuIds.length > 0){
            List<SxtJueseCaidan> jueseCaidanList = new ArrayList<>();
            for(Long menuId : menuIds){
                SxtJueseCaidan sxtJueseCaidan = new SxtJueseCaidan();
                sxtJueseCaidan.setRoleId(sxtJuese.getRoleId());
                sxtJueseCaidan.setMenuId(menuId);
                jueseCaidanList.add(sxtJueseCaidan);
            }
            sxtJueseCaidanMapper.batchJueseCaidan(jueseCaidanList);
        }
    }

    /**
     * 修改角色信息
     * 
     * @param sxtJuese 角色信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSxtJuese(SxtJuese sxtJuese)
    {
        sxtJuese.setUpdateTime(DateUtils.getNowDate());
        sxtJueseCaidanMapper.deleteSxtJueseCaidanByRoleId(sxtJuese.getRoleId());
        insertJueseCaidan(sxtJuese);
        return sxtJueseMapper.updateSxtJuese(sxtJuese);
    }

    /**
     * 批量删除角色信息
     * 
     * @param roleIds 需要删除的角色信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSxtJueseByRoleIds(Long[] roleIds)
    {
        for (Long roleId : roleIds)
        {
            SxtJuese sxtJuese = sxtJueseMapper.selectSxtJueseByRoleId(roleId);
            if (sxtYonghuJueseMapper.countJueseCaidanByJueseId(roleId)> 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", sxtJuese.getRoleName()));
            }
        }
        // 删除角色与菜单关联
        sxtJueseCaidanMapper.deleteSxtJueseCaidanByRoleIds(roleIds);
        return sxtJueseMapper.deleteJueseByIds(roleIds);
    }

    @Override
    public int deleteAuthUsers(Long roleId, Long[] userIds) {
        return sxtYonghuJueseMapper.deleteUserRoleInfos(roleId, userIds);
    }

    @Override
    public int deleteAuthUser(SxtYonghuJuese sxtYonghuJuese) {
        return sxtYonghuJueseMapper.deleteUserRoleInfo(sxtYonghuJuese);
    }


    /**
     * 删除角色信息信息
     * 
     * @param roleId 角色信息主键
     * @return 结果
     */
    @Override
    public int deleteSxtJueseByRoleId(Long roleId)
    {
        return sxtJueseMapper.deleteSxtJueseByRoleId(roleId);
    }
}
